package org.vectrics.recruiter.struts.admin;

import java.io.InputStream;
import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.Resume;
import org.vectrics.user.Person;

public class UploadResumeHelper {
	private transient Logger log = Logger.getLogger(this.getClass()); 

	public UploadResumeHelper() {
		super();
	}


	public void execute(InputStream uploadStream, String resumeName, String originalFileName, Person person) {
		try {
		    CandidateInfo candidate = RecruitServiceLocator.getRecruitUserService().createCandidateInfoIfNotExist(person);
		    if (candidate.getId() == null) {
		    	RecruitServiceLocator.getRecruitUserService().updateCandidateInfo(candidate);
		    }
	
			Resume resume = candidate.addResume();
			resume.setName(resumeName);
			
			RecruitServiceLocator.getRecruitUserService().saveResumeToFile(
					candidate, resume, originalFileName, uploadStream);
			ThreadProperties.forcedCommit();
			if (resume.getDocument().getSavedFileName() == null)
				throw new SystemException("No filename found in resume after upload");
			log.info("File saved successfully");
		} catch (Exception ex) {
			log.error("Exception saving file to server drive", ex);
			throw new SystemException("Exception saving file to server drive", ex);
		}
	}
	
}
